<?php
/**
 * 教室信息类
 */
namespace app\admin\controller;

use app\admin\model\Equipmentmodel;
use think\App;
use think\facade\View;
use app\admin\model\Strategymodel;
use app\admin\model\Usermodel;
use app\admin\model\Classroom;
use app\admin\model\Classifymodel;
use app\admin\model\Classtype;
use app\admin\model\Classgrade;
use app\admin\validate\Strategy as validate;

class Strategy extends Common
{
    protected $id;
    protected $cfmodel;
    protected $tpmodel;
    protected $grmodel;
    protected $croommodel;
    protected $usermodel;
    protected $equipment;

    public function __construct(App $app)
    {
        $this->validate = validate::class;
        $this->localModel = new Strategymodel();
        parent::__construct($app);
        $this->tpmodel = new Classtype();
        $this->grmodel = new Classgrade();
        $this->cfmodel = new Classifymodel();
        $this->croommodel = new Classroom();
        $this->usermodel = new Usermodel();
        $this->equipment = new Equipmentmodel();
        $this->id = $this->request->param('id', 0);
    }

    public function index()
    {
        $where = $showDataArr = [];

        $dataArr = $this->request->request('search/a', []);

        if($dataArr['title'])  $where[]          = ['title', 'like', "%".$dataArr['title']."%"];
        if($dataArr['status'])  $where[]         = ['status', '=', $dataArr['status']];
        if($dataArr['device_type'])  $where[]    = ['device_type', '=', $dataArr['device_type']];
        if($dataArr['examine_status'])  $where[] = ['examine_status', '=', $dataArr['examine_status']];

        if(session('adminUserInfo.cid')){
            $dataArr['croomInfo'] = $this->croommodel->findDetaiInfoById('type, level', session('adminUserInfo.cid'));
            $where[] = ['ctid', '=', $dataArr['croomInfo']['type']];
            $where[] = ['gid', '=', $dataArr['croomInfo']['level']];
        }

        $list = $this->localModel->findAllPageInfo('*', $where);
        View::assign(['data'=>$list['data'], 'pageData'=>$list['page'], 'showData'=>$dataArr]);
    }

    public function add(){

        $showData['tpData'] = $this->tpmodel->findAllInfo('id, name', [['status', '=', 1]]);
        $showData['grData'] = $this->grmodel->findAllInfo('id, name', [['status', '=', 1]]);
        $showData['cfData'] = $this->cfmodel->findAllInfo('id, name', [['status', '=', 1], ['fid', '=', 22]]);

        return $showData;
    }

    public function save(){
        $dataArr = $this->request->request('info/a', []);
        $otherArr = $this->request->request('detail/a', []);
        $equipmentArr = $this->request->request('equipment/a', []);//print_r($equipmentArr);exit;

        if($dataArr['title'])  $info = $this->localModel->findDetaiInfoByOthers('id', ['title'=>$dataArr['title'], 'is_delete'=>1]);
        if(($this->id && $info && $this->id != $info[0]['id']) || (!$this->id && $info))  return json(['result'=>'fail', 'title'=>'标题已存在,请重新填写！']);
        if($msg = $this->checkData($dataArr, $this->validateType))  return json(['result'=>'fail', 'title'=>$msg]);
        if(session('adminUserInfo.cid') && !count($equipmentArr))  return json(['result'=>'fail', 'title'=>'请选择设备']);

        $res = $this->localModel->saveInfo($dataArr, $equipmentArr, $otherArr, $this->id);

        if(!$res)  return json(['result'=>'fail', 'title'=>'操作失败！']);

        $this->logsData = ['cid'=>$this->id ? $this->id : $res, 'action'=>$this->id ? 'edit' : 'add', 'msg'=>$dataArr['title']];
        return json(['result'=>'success', 'title'=>'恭喜您操作成功！', 'url'=>(string)url($this->request->controller().'/index')]);
    }


}
